export function setupCounter(element) {
  let counter = 0

  fetch('/info').then(async (res) => {
    try {
        const data = await res.json();
        console.log(data)
        const visited = data.visited || 0;
        setCounter(visited);
    } catch(err) {
        console.log(err)
    }
  });

  const setCounter = (count) => {
    counter = count
    element.innerHTML = `访问人数: ${counter}`
  }
  element.addEventListener('click', () => {
    setCounter(counter + 1);

    setTimeout(() => {
      element.innerHTML = '正在前往少年派奇幻漂流~';
    }, 500);

    setTimeout(() =>{
      location.href = "https://www.hanxiaoxin.cn/pi/";
    }, 3000);
  })
  setCounter('-')
}
